//
//  HQQReachabilityStatusManager.m
//  HQQNetworkExemple
//
//  Created by 黄强强 on 16/7/7.
//  Copyright © 2016年 黄强强. All rights reserved.
//

#import "HQQReachabilityManager.h"
#import <AFNetworkReachabilityManager.h>

@implementation HQQReachabilityManager

NSString * const HQQNetworkReachabilityStatusChangedNotification = @"HQQNetworkReachabilityStatusChangedNotification";
NSString * const HQQNetworkReachabilityStatusKey = @"HQQNetworkReachabilityStatusKey";

+ (HQQNetworkReachabilityStatus)currentNetworkingStatus
{
    switch ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus) {
        case AFNetworkReachabilityStatusUnknown:
            return HQQNetworkReachabilityStatusUnknown;
            break;
            
        case AFNetworkReachabilityStatusNotReachable:
            return HQQNetworkReachabilityStatusNotReachable;
            
            break;
            
        case AFNetworkReachabilityStatusReachableViaWWAN:
            return HQQNetworkReachabilityStatusReachableViaWWAN;
            
            break;
            
        case AFNetworkReachabilityStatusReachableViaWiFi:
            return HQQNetworkReachabilityStatusReachableViaWiFi;
            
            break;
        default:
            return HQQNetworkReachabilityStatusUnknown;
            break;
    }
}

+ (void)startMonitoring
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                
                [[NSNotificationCenter defaultCenter] postNotificationName:HQQNetworkReachabilityStatusChangedNotification object:self userInfo:@{HQQNetworkReachabilityStatusKey:@(HQQNetworkReachabilityStatusUnknown)}];
                
                break;
                
            case AFNetworkReachabilityStatusNotReachable:
                
                [[NSNotificationCenter defaultCenter] postNotificationName:HQQNetworkReachabilityStatusChangedNotification object:self userInfo:@{HQQNetworkReachabilityStatusKey:@(HQQNetworkReachabilityStatusNotReachable)}];
                
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                
                [[NSNotificationCenter defaultCenter] postNotificationName:HQQNetworkReachabilityStatusChangedNotification object:self userInfo:@{HQQNetworkReachabilityStatusKey:@(HQQNetworkReachabilityStatusReachableViaWWAN)}];
                
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                
                [[NSNotificationCenter defaultCenter] postNotificationName:HQQNetworkReachabilityStatusChangedNotification object:self userInfo:@{HQQNetworkReachabilityStatusKey:@(HQQNetworkReachabilityStatusReachableViaWiFi)}];
                
                break;
            default:
                break;
        }
    }];
    
    // 开始监听
    [manager startMonitoring];
}

@end
